package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Course;
import com.example.demo.mapper.BookMapper;
import com.example.demo.mapper.CourseMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Resource
    CourseMapper courseMapper;


    //("/add")
    @PostMapping//接口写好了,前台传递数据。
    public Result<?> save(@RequestBody Course Course) {
       
        courseMapper.insert(Course);//插入数据库
        System.out.println("TEST: "+Course);
        return Result.success();
    }


    @PutMapping
    public Result<?> update(@RequestBody Course course) {
        //映射成对象实体

        courseMapper.updateById(course);
        //修改数据库
        return Result.success();
    }


    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        //映射成对象实体
        courseMapper.deleteById(id);
        //修改数据库
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(courseMapper.selectById(id));
    }

    //("/selectAllUsers")
    @GetMapping//接口写好了,前台传递数据。
    // @ResponseBody
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(search)) {
            queryWrapper.like("day5", search);

        }
       /* if (StrUtil.isNotBlank(search)) {
            queryWrapper.like("day4", search);
        }*/
        System.out.println(search);
        Page<Course> userPage = courseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(userPage);
    }
}

